//
//  SettingViewController.m
//  Coco_Music
//
//  Created by K。 on 15/7/1.
//  Copyright (c) 2015年 寇广超. All rights reserved.
//

#import "SettingViewController.h"

@interface SettingViewController ()

@end

@implementation SettingViewController
-(void)dealloc
{
    [_tView release];
    [_cell release];
    [_cache release];
    [super dealloc];
  
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.myArr =[NSMutableArray arrayWithObjects:@"我的收藏",@"清除缓存",@"意见反馈",@"关于我们",nil];
        
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_back.png"] style:UIBarButtonItemStylePlain target:self action:@selector(GoBack)];
    UILabel *set = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 30, 30)];
    set.font = [UIFont fontWithName:@"SXSLKF" size:22];
   set.text = @"设 置";
    self.navigationItem.titleView = set;
    
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 180)];
    image.image = [UIImage imageNamed:@"image3"];
    [self.view addSubview:image];
    self.tView = [[UITableView alloc] initWithFrame:CGRectMake(0, 244, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    _tView.delegate = self;
    _tView.dataSource = self;
    _tView.separatorStyle = UITableViewCellSelectionStyleNone;
  
    [self.view addSubview:_tView];

    [_tView release];
}
-(void)GoBack
{
    
   [self dismissViewControllerAnimated:YES completion:^{
       
       
   }];
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!self.cell) {
       self.cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@" cell"];
    }
    self.cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    self.cell.textLabel.text = [self.myArr objectAtIndex:indexPath.row];
    self.cell.textLabel.font = [UIFont fontWithName:@"SXSLKF" size:20];
    self.cell.nightBackgroundColor = [UIColor lightGrayColor];
    if (indexPath.row == 0) {
        self.cell.imageView.image = [UIImage imageNamed:@"s0"];
        
    }
    if (indexPath.row == 1) {
        self.cell.accessoryType = UITableViewCellAccessoryNone;
        self.cache = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 80, 10, 80, 20)];
        _cache.text = [self sizeCache];
        _cache.font = [UIFont boldSystemFontOfSize:12];
        _cache.textColor = [UIColor blackColor];
        [self.cell addSubview:_cache];
        self.cell.imageView.image = [UIImage imageNamed:@"s1"];
    }
    if (indexPath.row == 2) {
       self.cell.imageView.image = [UIImage imageNamed:@"s2"];
        
    }
    if (indexPath.row == 3) {
        self.cell.imageView.image = [UIImage imageNamed:@"s3"];
        
    }
    
    
    return self.cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _myArr.count;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

//    [self.tView deselectRowAtIndexPath:indexPath animated:YES];
    
    // 我的收藏
    if (indexPath.row == 0) {
        FavoriteViewController *favourite = [[FavoriteViewController alloc] init];
        [self.navigationController pushViewController:favourite animated:YES];
        [favourite release];
    }
    // 清除缓存
    if (indexPath.row == 1) {
        UIAlertView *cacheAlert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"您确定要清除缓存吗" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        
        [cacheAlert show];
        [cacheAlert release];
    }
    
    // 意见反馈,邮件发送
    if (indexPath.row == 2) {
        SendMessageController *send = [[SendMessageController alloc] init];
        [self.navigationController pushViewController:send animated:YES];
        [send release];
    
    }
    if (indexPath.row == 3) {
        AboutViewController *about = [[AboutViewController alloc] init];
        [self.navigationController pushViewController:about animated:YES];
        [about release];
    }
    }
#pragma 清除缓存

// 计算缓存大小
- (NSString *)sizeCache
{
    NSUInteger size = [[SDImageCache sharedImageCache]getSize];
    CGFloat mb = size / 1024.0 / 1024.0;
    NSString *string = [NSString stringWithFormat:@"缓存:%.2fMB", mb];
    
    return string;
}


// 清除缓存的方法
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        
            [[SDImageCache sharedImageCache]clearDisk];
        
    }
    [self performSelectorOnMainThread:@selector(clearCacheSuccess) withObject:nil waitUntilDone:YES];


}
// 清理缓存最后调用的方法
- (void)clearCacheSuccess
{
    [self.cache setText:@"0.0MB"];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
